﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        double a = 0;
        double b = 0;
        double l = 0;
        bool t = false;
        string f;
        public Form1()
        {

            InitializeComponent();
        }
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.NumPad1)
            {
                this.button1.PerformClick();
                //this.btnTempTest.PerformClick();  
            }
            if (keyData == Keys.NumPad2)
            {
                this.button2.PerformClick();
            }
            if (keyData == Keys.NumPad3)
            {
                this.button3.PerformClick();
            }
            if (keyData == Keys.NumPad4)
            {
                this.button4.PerformClick();
            }
            if (keyData == Keys.NumPad5)
            {
                this.button5.PerformClick();
            }
            if (keyData == Keys.NumPad6)
            {
                this.button6.PerformClick();
            }
            if (keyData == Keys.NumPad7)
            {
                this.button7.PerformClick();
            }
            if (keyData == Keys.NumPad8)
            {
                this.button8.PerformClick();
            }
            if (keyData == Keys.NumPad9)
            {
                this.button9.PerformClick();
            }
            if (keyData == Keys.NumPad0)
            {
                this.button10.PerformClick();
            }
            if (keyData == Keys.D0)
            {
                this.button10.PerformClick();
            }
            if (keyData == Keys.D9)
            {
                this.button9.PerformClick();
            }
            if (keyData == Keys.D8)
            {
                this.button8.PerformClick();
            }
            if (keyData == Keys.D7)
            {
                this.button7.PerformClick();
            }
            if (keyData == Keys.D6)
            {
                this.button6.PerformClick();
            }
            if (keyData == Keys.D5)
            {
                this.button5.PerformClick();
            }
            if (keyData == Keys.D4)
            {
                this.button4.PerformClick();
            }
            if (keyData == Keys.D3)
            {
                this.button3.PerformClick();
            }
            if (keyData == Keys.D2)
            {
                this.button2.PerformClick();
            }
            if (keyData == Keys.D1)
            {
                this.button1.PerformClick();
            }
            if (keyData == Keys.Back)
            {
                this.button13.PerformClick();
            }
            if (keyData == Keys.Add)
            {
                this.button15.PerformClick();
            }
            if (keyData == Keys.Subtract)
            {
                this.button16.PerformClick();
            }
            if (keyData == Keys.Multiply)
            {
                this.button17.PerformClick();
            }
            if (keyData == Keys.Divide)
            {
                this.button18.PerformClick();
            }
            if (keyData == Keys.Decimal)
            {
                this.button12.PerformClick();
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }
        private void button3_Click(object sender, EventArgs e)
        {
            if (t == true)
            {
                label1.Text = "";
                t = false;
            }
            label1.Text += "3";
        }
        private void button4_Click(object sender, EventArgs e)
        {
            if (t == true)
            {
                label1.Text = "";
                t = false;
            }
            label1.Text += "4";
        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (t == true)
            {
                label1.Text = "";
                t = false;
            }
            label1.Text += "1";
        }
        private void button2_Click(object sender, EventArgs e)
        {
            if (t == true)
            {
                label1.Text = "";
                t = false;
            }
            label1.Text += "2";
        }
        private void button5_Click(object sender, EventArgs e)
        {
            if (t == true)
            {
                label1.Text = "";
                t = false;
            }
            label1.Text += "5";
        }
        private void button6_Click(object sender, EventArgs e)
        {
            if (t == true)
            {
                label1.Text = "";
                t = false;
            }
            label1.Text += "6";
        }
        private void button7_Click(object sender, EventArgs e)
        {
            if (t == true)
            {
                label1.Text = "";
                t = false;
            }
            label1.Text += "7";
        }
        private void button8_Click(object sender, EventArgs e)
        {
            if (t == true)
            {
                label1.Text = "";
                t = false;
            }
            label1.Text += "8";
        }
        private void button9_Click(object sender, EventArgs e)
        {
            if (t == true)
            {
                label1.Text = "";
                t = false;
            }
            label1.Text += "9";
        }
        private void button10_Click(object sender, EventArgs e)
        {
            if (t == true)
            {
                if (f == "/")
                {
                    MessageBox.Show("0不能为除数", "错误");
                }
                else
                {
                    label1.Text = "";
                    t = false;
                }
            }
            else
            {
                label1.Text += "0";
            } 
        }
        private void button12_Click(object sender, EventArgs e)
        {
            if (label1.Text.Contains('.'))
            {
                MessageBox.Show("只能有一个小数点", "错误 ");
            }
            else
            {
                if (label1.Text == "")
                {
                    MessageBox.Show("小数点不能作为开头", "错误");                
                }
                else
                {
                    label1.Text += '.';

                }
                 
            }
        }
        private void button13_Click(object sender, EventArgs e)
        {
            if (label1.Text.Length > 0)
                label1.Text = label1.Text.Remove(label1.Text.Length - 1, 1);
        }
        private void button14_Click(object sender, EventArgs e)
        {
            label1.Text = "";
            label2.Text = "";
            a = 0;
            b = 0;
            f = null;
            t = false;
        }
        private void button15_Click(object sender, EventArgs e)
        {
            if (f == null)
            {
                if (label1.Text.Length > 0)
                {
                    a = double.Parse(label1.Text);
                    l = a;
                    f = "+";
                    t = true;
                }
                else
                {
                    MessageBox.Show("没有内容", "错误");
                }
            }
            else
            {
                switch (f)
                {
                    case "+":

                        b = a + double.Parse(label1.Text);
                        break;
                    case "-":

                        b = a - double.Parse(label1.Text);
                        break;
                    case "*":
                        l = a;
                        b = a * double.Parse(label1.Text);
                        break;
                    case "/":

                        b = a / double.Parse(label1.Text);
                        break;
                    case "x2":

                        b = a * double.Parse(label1.Text);
                        break;
                    case "sqrt":

                        b = Math.Sqrt(a);
                        break;
                    case "log":

                        b = Math.Log(double.Parse(label1.Text), a);
                        break;
                    case "ln":

                        b = Math.Log(a, Math.E);
                        break;
                }
                label1.Text = b + "";
                label2.Text = l + "";
                if (label1.Text.Length > 0)
                {
                    a = double.Parse(label1.Text);
                    l = a;
                    f = "+";
                    t = true;
                }
                else
                {
                    MessageBox.Show("没有内容", "错误");
                }
            }
        }
        private void button16_Click(object sender, EventArgs e)
        {
            if (f == null)
            {
                if (label1.Text.Length > 0)
                {
                    a = double.Parse(label1.Text);
                    l = a;
                    f = "-";
                    t = true;
                }
                else
                {
                    MessageBox.Show("没有内容", "错误");
                }
            }
            else
            {
                switch (f)
                {
                    case "+":

                        b = a + double.Parse(label1.Text);
                        break;
                    case "-":

                        b = a - double.Parse(label1.Text);
                        break;
                    case "*":
                        l = a;
                        b = a * double.Parse(label1.Text);
                        break;
                    case "/":

                        b = a / double.Parse(label1.Text);
                        break;
                    case "x2":

                        b = a * double.Parse(label1.Text);
                        break;
                    case "sqrt":

                        b = Math.Sqrt(a);
                        break;
                    case "log":

                        b = Math.Log(double.Parse(label1.Text), a);
                        break;
                    case "ln":

                        b = Math.Log(a, Math.E);
                        break;
                }
                label1.Text = b + "";
                label2.Text = l + "";
                if (label1.Text.Length > 0)
                {
                    a = double.Parse(label1.Text);
                    l = a;
                    f = "-";
                    t = true;
                }
                else
                {
                    MessageBox.Show("没有内容", "错误");
                }
            }
        }
        private void button17_Click(object sender, EventArgs e)
        {
            if (f == null)
            {
                if (label1.Text.Length > 0)
                {
                    a = double.Parse(label1.Text);
                    l = a;
                    f = "*";
                    t = true;
                }
                else
                {
                    MessageBox.Show("没有内容", "错误");
                }
            }
            else
            {
                switch (f)
                {
                    case "+":

                        b = a + double.Parse(label1.Text);
                        break;
                    case "-":

                        b = a - double.Parse(label1.Text);
                        break;
                    case "*":
                        l = a;
                        b = a * double.Parse(label1.Text);
                        break;
                    case "/":

                        b = a / double.Parse(label1.Text);
                        break;
                    case "x2":

                        b = a * double.Parse(label1.Text);
                        break;
                    case "sqrt":

                        b = Math.Sqrt(a);
                        break;
                    case "log":

                        b = Math.Log(double.Parse(label1.Text), a);
                        break;
                    case "ln":

                        b = Math.Log(a, Math.E);
                        break;
                }
                label1.Text = b + "";
                label2.Text = l + "";
                if (label1.Text.Length > 0)
                {
                    a = double.Parse(label1.Text);
                    l = a;
                    f = "*";
                    t = true;
                }
                else
                {
                    MessageBox.Show("没有内容", "错误");
                }
            }
        }
        private void button18_Click(object sender, EventArgs e)
        {
            if (f == null)
            {
                if (label1.Text.Length > 0)
                {
                    a = double.Parse(label1.Text);
                    l = a;
                    f = "/";
                    t = true;
                }
                else
                {
                    MessageBox.Show("没有内容", "错误");
                }
            }
            else
            {
                switch (f)
                {
                    case "+":

                        b = a + double.Parse(label1.Text);
                        break;
                    case "-":

                        b = a - double.Parse(label1.Text);
                        break;
                    case "*":
                        l = a;
                        b = a * double.Parse(label1.Text);
                        break;
                    case "/":

                        b = a / double.Parse(label1.Text);
                        break;
                    case "x2":

                        b = a * double.Parse(label1.Text);
                        break;
                    case "sqrt":

                        b = Math.Sqrt(a);
                        break;
                    case "log":

                        b = Math.Log(double.Parse(label1.Text), a);
                        break;
                    case "ln":

                        b = Math.Log(a, Math.E);
                        break;
                }
                label1.Text = b + "";
                label2.Text = l + "";
                if (label1.Text.Length > 0)
                {
                    a = double.Parse(label1.Text);
                    l = a;
                    f = "/";
                    t = true;
                }
                else
                {
                    MessageBox.Show("没有内容", "错误");
                }
            }
        }
        private void button19_Click(object sender, EventArgs e)
        {
            if (f == null)
            {
                if (label1.Text.Length > 0)
                {
                    a = double.Parse(label1.Text);
                    l = a;
                    f = "x2";
                    t = true;
                }
                else
                {
                    MessageBox.Show("没有内容", "错误");
                }
            }
            else
            {
                switch (f)
                {
                    case "+":

                        b = a + double.Parse(label1.Text);
                        break;
                    case "-":

                        b = a - double.Parse(label1.Text);
                        break;
                    case "*":
                        l = a;
                        b = a * double.Parse(label1.Text);
                        break;
                    case "/":

                        b = a / double.Parse(label1.Text);
                        break;
                    case "x2":

                        b = a * double.Parse(label1.Text);
                        break;
                    case "sqrt":

                        b = Math.Sqrt(a);
                        break;
                    case "log":

                        b = Math.Log(double.Parse(label1.Text), a);
                        break;
                    case "ln":

                        b = Math.Log(a, Math.E);
                        break;
                }
                label1.Text = b + "";
                label2.Text = l + "";
                if (label1.Text.Length > 0)
                {
                    a = double.Parse(label1.Text);
                    l = a;
                    f = "x2";
                    t = true;
                }
                else
                {
                    MessageBox.Show("没有内容", "错误");
                }
            }
        }
        private void button20_Click(object sender, EventArgs e)
        {
            if (f == null)
            {
                if (label1.Text.Length > 0)
                {
                    a = double.Parse(label1.Text);
                    l = a;
                    f = "sqrt";
                    t = true;
                }
                else
                {
                    MessageBox.Show("没有内容", "错误");
                }
            }
            else
            {
                switch (f)
                {
                    case "+":

                        b = a + double.Parse(label1.Text);
                        break;
                    case "-":

                        b = a - double.Parse(label1.Text);
                        break;
                    case "*":
                        l = a;
                        b = a * double.Parse(label1.Text);
                        break;
                    case "/":

                        b = a / double.Parse(label1.Text);
                        break;
                    case "x2":

                        b = a * double.Parse(label1.Text);
                        break;
                    case "sqrt":

                        b = Math.Sqrt(a);
                        break;
                    case "log":

                        b = Math.Log(double.Parse(label1.Text), a);
                        break;
                    case "ln":

                        b = Math.Log(a, Math.E);
                        break;
                }
                label1.Text = b + "";
                label2.Text = l + "";
                if (label1.Text.Length > 0)
                {
                    a = double.Parse(label1.Text);
                    l = a;
                    f = "sqrt";
                    t = true;
                }
                else
                {
                    MessageBox.Show("没有内容", "错误");
                }
            }
        }
        private void button22_Click(object sender, EventArgs e)
        {
            if (f == null)
            {
                if (label1.Text.Length > 0)
                {
                    a = double.Parse(label1.Text);
                    l = a;
                    f = "log";
                    t = true;
                }
                else
                {
                    MessageBox.Show("没有内容", "错误");
                }
            }
            else
            {
                switch (f)
                {
                    case "+":

                        b = a + double.Parse(label1.Text);
                        break;
                    case "-":

                        b = a - double.Parse(label1.Text);
                        break;
                    case "*":
                        l = a;
                        b = a * double.Parse(label1.Text);
                        break;
                    case "/":

                        b = a / double.Parse(label1.Text);
                        break;
                    case "x2":

                        b = a * double.Parse(label1.Text);
                        break;
                    case "sqrt":

                        b = Math.Sqrt(a);
                        break;
                    case "log":

                        b = Math.Log(double.Parse(label1.Text), a);
                        break;
                    case "ln":

                        b = Math.Log(a, Math.E);
                        break;
                }
                label1.Text = b + "";
                label2.Text = l + "";
                if (label1.Text.Length > 0)
                {
                    a = double.Parse(label1.Text);
                    l = a;
                    f = "log";
                    t = true;
                }
                else
                {
                    MessageBox.Show("没有内容", "错误");
                }
            }
        }
        private void button21_Click(object sender, EventArgs e)
        {
            if (f == null)
            {
                if (label1.Text.Length > 0)
                {
                    a = double.Parse(label1.Text);
                    l = a;
                    f = "ln";
                    t = true;
                }
                else
                {
                    MessageBox.Show("没有内容", "错误");
                }
            }
            else
            {
                switch (f)
                {
                    case "+":

                        b = a + double.Parse(label1.Text);
                        break;
                    case "-":

                        b = a - double.Parse(label1.Text);
                        break;
                    case "*":
                        l = a;
                        b = a * double.Parse(label1.Text);
                        break;
                    case "/":

                        b = a / double.Parse(label1.Text);
                        break;
                    case "x2":

                        b = a * double.Parse(label1.Text);
                        break;
                    case "sqrt":

                        b = Math.Sqrt(a);
                        break;
                    case "log":

                        b = Math.Log(double.Parse(label1.Text), a);
                        break;
                    case "ln":

                        b = Math.Log(a, Math.E);
                        break;
                }
                label1.Text = b + "";
                label2.Text = l + "";
                if (label1.Text.Length > 0)
                {
                    a = double.Parse(label1.Text);
                    l = a;
                    f = "ln";
                    t = true;
                }
                else
                {
                    MessageBox.Show("没有内容", "错误");
                }
            }
        }
        private void button11_Click(object sender, EventArgs e)
        {
            if (label1.Text.Length > 0)
            {

                switch (f)
                {
                    case "+":

                        b = a + double.Parse(label1.Text);
                        break;
                    case "-":

                        b = a - double.Parse(label1.Text);
                        break;
                    case "*":
                        l = a;
                        b = a * double.Parse(label1.Text);
                        break;
                    case "/":

                        b = a / double.Parse(label1.Text);
                        break;
                    case "x2":

                        b = a * double.Parse(label1.Text);
                        break;
                    case "sqrt":

                        b = Math.Sqrt(a);
                        break;
                    case "log":

                        b = Math.Log(double.Parse(label1.Text), a);
                        break;
                    case "ln":

                        b = Math.Log(a, Math.E);
                        break;
                }

                label1.Text = b + "";
                label2.Text = l + "";
                t = true;
            }
        }

        private void button23_Click(object sender, EventArgs e)
        {
            new Form2().Show();
        }
    }
}                            